// 格式化 JSON
function formatJSON() {
    const input = document.getElementById("jsonInput").value;
    try {
        const parsedJSON = JSON.parse(input);
        const formattedJSON = JSON.stringify(parsedJSON, null, 4);
        document.getElementById("jsonInput").value = formattedJSON;
    } catch (error) {
        showToast("无效的 JSON：" + error.message);
    }
}

// 清空 JSON
function clearJSON() {
    document.getElementById("jsonInput").value = "";
}

// 复制 JSON 到剪贴板
function copyJSON() {
    const jsonContent = document.getElementById("jsonInput").value;
    navigator.clipboard.writeText(jsonContent).then(() => {
        showToast("JSON 已复制到剪贴板");
    });
}

// 导出 JSON 为文件
function exportJSON() {
    const jsonContent = document.getElementById("jsonInput").value;
    const blob = new Blob([jsonContent], { type: "application/json" });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = "formatted.json";
    link.click();
    URL.revokeObjectURL(url);
}

// ��示提示框
function showToast(message) {
    const toast = document.getElementById("toast");
    toast.textContent = message;
    toast.classList.add("show");
    setTimeout(() => {
        toast.classList.remove("show");
    }, 2000);
} 